package com.pactera.madp.cp.controller.oms.activity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.activity.ActivityDto;
import com.pactera.madp.cp.api.dto.activity.ActivitySaveDto;
import com.pactera.madp.cp.api.dto.activity.ActivityUpdateDto;
import com.pactera.madp.cp.api.entity.activity.ActivityEntity;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.IActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@AllArgsConstructor
@RequestMapping("/activity")
@Api(value = "activity", tags = "活动详情")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    @ApiModelProperty("新增活动")
    @PostMapping("/save")
    public R save(@RequestBody ActivitySaveDto dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        ActivityEntity entity = new ActivityEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setActStatus(0);
        entity.setCorpId(corpId);
        entity.setStartTime(dto.getStartTime().atStartOfDay());
        entity.setEndTime(dto.getEndTime().atStartOfDay());
        boolean save = activityService.save(entity);
        return save? R.ok("success") : R.failed("failed");
    }

    @ApiModelProperty("查询活动")
    @GetMapping("/query")
    public R query(Page page, ActivityDto dto) {
        IPage iPage = activityService.queryByDto(page, dto);
        return R.ok(iPage);
    }

    @ApiModelProperty("删除活动")
    @DeleteMapping("/delete")
    public R delete(Integer id) {
        boolean remove = activityService.removeById(id);
        return remove? R.ok("success") : R.failed("failed");
    }

    @ApiModelProperty("编辑活动")
    @PutMapping("/update")
    public R update(@RequestBody ActivitySaveDto dto) {

        Boolean update = activityService.compile(dto);
        return update? R.ok("success") : R.failed("failed");
    }

    @ApiModelProperty("更改活动状态")
    @PutMapping("/audit")
    public R audit(@RequestBody ActivityUpdateDto dto) {
        Boolean disapprove = activityService.audit(dto.getIds(), dto.getActStatus(), dto.getActReason());
        return disapprove? R.ok("success") : R.failed("failed");
    }

    @ApiModelProperty("活动跳转")
    @GetMapping("/attend")
    public R attend(Integer contactId, String url, HttpServletResponse response) {
        return activityService.attend(contactId, url, response);

    }

}
